package com.yanqu.road.dao.impl.activity.advertisement;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.advertisement.config.AdvertisementSystemConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AdvertisementActivityConfigImpl extends TempDao {

    private static String getAdvertisementConfigSql = "select * from `t_s_activity_system_ad` where `activityId`=? ORDER BY `id` ASC";
    public Map<Integer, AdvertisementSystemConfig> getAdvertisementConfigMap(int activityId) {
        Map<Integer, AdvertisementSystemConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getAdvertisementConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                AdvertisementSystemConfig advertisementSystemConfig = new AdvertisementSystemConfig();
                advertisementSystemConfig.setActivityId(rs.getInt("activityId"));
                advertisementSystemConfig.setSystemId(rs.getInt("id"));
                advertisementSystemConfig.setName(rs.getString("name"));
                advertisementSystemConfig.setOrder(rs.getInt("order"));
                advertisementSystemConfig.setButtonFinish(rs.getInt("buttonFinish"));
                advertisementSystemConfig.setNumMax(rs.getInt("numMax"));
                advertisementSystemConfig.setCoolCd(rs.getInt("coolCd"));
                advertisementSystemConfig.setReward(rs.getString("reward"));
                advertisementSystemConfig.setSpecialReward(rs.getString("specialReward"));
                advertisementSystemConfig.setShopId(rs.getInt("shopId"));
                map.put(advertisementSystemConfig.getSystemId(), advertisementSystemConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
